package org.moonshot.util;

import org.moonshot.log.LogFactory;
import org.moonshot.log.LogToken;
import org.moonshot.log.Logger;

/**
 * I am a utility class, part of the standard MoonShot Software Java utilities. Like all
 * utility classes, I wrap routine but lengthy tasks in a type-safe manner.
 * 
 * @author Tiogshi Laj
 */
public class Reflection
{
	private static final Logger logger = LogFactory.create(Reflection.class);
	
    @SuppressWarnings("unchecked")
    public static<T> T createObject(String aClassName, Class<T> aSignature, LogToken aToken)
    {
        T retval = null;
        try
        {
            Class<? extends T> theClass = (Class<? extends T>) Class.forName (aClassName);
            if( aSignature.isAssignableFrom (theClass) )
            {
                retval = theClass.getConstructor ().newInstance ();
            }
        }
        catch (Exception e)
        {
            logger.error(aToken, e);
        }
        
        return retval;
    }
}
